<?

	include("inc/config.inc.php");

	include("inc/header.inc.php");

	echo "<h2><span class='textColorChange'>$config[systemtitle]</span></h2><br/>\n\n";

	// First system user processing
	if ($config[firstAdmin] == $_SESSION[user])
	{
		$result = mysqli_query($conn, "SELECT * FROM users WHERE user='$_SESSION[user]'");
		if (mysqli_num_rows($result) == 0)
			mysqli_query($conn, "INSERT INTO users SET user='$_SESSION[user]', prestaged=2, role='HR'");
		}


	// Check if user exists in database and has been set up by HR
	$result = mysqli_query($conn, "SELECT * FROM users WHERE user='$_SESSION[user]' AND prestaged != 1");

	if (mysqli_num_rows($result) == 0 && isset($_SESSION[user]))
	{
		// User account isn't in database and not in prestaged state
		echo "<br/>Your account still needs to be setup by HR. They have been notified of this.<br/>\n";
		mysqli_query($conn, "INSERT INTO users SET user='$_SESSION[user]', prestaged=1");
		niceMail($config[hrEmail], "Account setup required for ". $_SESSION[user], $_SESSION[user] . " has just tried to log into $config[systemtitle], but their account has not yet been setup fully.\n\rPlease visit ". $config[url] ." to setup the account for them.");
		unset($_SESSION[user]);
	}

	else
	{
		$user = $result->fetch_assoc();

		if ($user[role] != "HR")
			echo "<a href='login.php?a=logout' style='color:black;font-size:10pt'>[Logout]</a><br/>";

 // ---------- Show role based informaton ------------------------------------------------------------------

		// Show options for HR role
		if ($user[role]=="HR")
		{
			$_SESSION[role] = "HR";
			echo "<div style='background:#504E52' class='normalText'>\n";
			echo "<b>HR Options - $user[firstname] $user[lastname]</b><br/>\n";
			echo "<table border='0'><tr><td width='200' valign='top'>\n";
			echo "<a href='login.php?a=logout'><img src='img/arrow_undo.png' height='16' width='16' alt='Logout' />Logout</a><br/>\n";
			echo "<a href='users.php'><img src='img/group.png' height='16' width='16' alt='Manage Users' />Manage Users</a><br/>\n";
			echo "<a href='usernew.php'><img src='img/add.png' height='16' width='16' alt='Add New Users' />Add New User</a><br/>\n";
			echo "</td><td valign='top'>\n";
			echo "<a href='userview.php'><img src='img/user.png' height='16' width='16' alt='View User' />View User</a><br/>\n";
			echo "<a href='calendar.php'><img src='img/calendar.png' height='16' width='16' alt='Calendar' />Holiday Calendar</a><br/>\n";
			echo "<a href='bhol.php'><img src='img/calendar_edit.png' height='16' width='16' alt='Manage Bank Holidays' />Manage Bank Holidays</a><br/>\n";
			echo "<a href='chol.php'><img src='img/calendar_edit.png' height='16' width='16' alt='Manage Compulsory Holidays' />Manage Compulsory Holidays</a><br/>\n";
			echo "</td></tr></table>\n";
			echo "</div>\n";
			echo "\n<br/>\n\n";

			// Find how many prestaged accounts are waiting
			$prestagedWaiting = getprestagedCount();
			if ($prestagedWaiting > 0)
			{
				echo "<div style='background:#B6010A' class='normalText'>\n";
				echo "<b>Users requiring setup</b><br/>\n";
				echo "You have $prestagedWaiting users that require setup. <a href='users.php'>View &gt;&gt;</a><br/>\n";
				echo "</div>\n";
				echo "\n<br/>\n\n";
			}



		}


		// Show options for Manager role
		if ($user[role]=="Manager" || $user[role]=="HR")
		{
			if ($user[role]=="Manager")
				$_SESSION[role] = "Manager";

			// Find how many approvals are waiting
			$approvalsWaiting = getWaitingApprovalsCount();
			if ($approvalsWaiting > 0)
			{
				echo "<div style='background:#B6010A' class='normalText'>\n";
				echo "<b>Holiday Requests</b><br/>\n";
				echo "You have $approvalsWaiting holiday requests that require your approval. <a href='approvals.php'>View &gt;&gt;</a><br/>\n";
				echo "</div>\n";
				echo "\n<br/>\n\n";
			}
		}

 // ---------- My Entitlement Section ----------------------------------------------------------------------

		$daysUsedThis = 0;
		$daysUsedNext = 0;

		// Work out total entitlement
		$total = $user[carriedover] + $user[entitlement];
		$total = number_format($total, 1, '.', '');

		// Get days used for this year and next
		$daysUsedThis = daysUsed(date("Y"), $_SESSION[user]);
		$daysUsedNext = daysUsed(date("Y", strtotime("+1 year")), $_SESSION[user]);
		
		// Work out remaining days for this year and next
		$daysRemainThis = $total-$daysUsedThis;
		$daysRemainNext = $user[entitlement]-$daysUsedNext;

		$thisyear = date("Y");
		$nextyear = date("Y")+1;

		echo "<div style='background:#504E52' class='normalText'>\n";
		echo "<b>Holiday entitlement - $user[firstname] $user[lastname]</b>\n";
		echo "<br/><div width='100%' align='center'><table width='95%' cellspacing='10' border='0'>\n";
		echo "<tr><td width='50%' align='center'><div style='position:relative;left:-55px;top:10px'>$thisyear:</div><div style='line-height:1em;font-size:70px'>$daysRemainThis</div><div style='position:relative;left:70px;'>of $total remaining</div></td>\n";
		echo "<td width='50%' align='center'><div style='position:relative;left:-55px;top:10px'>$nextyear:</div><div style='line-height:1em;font-size:70px'>$daysRemainNext</div><div style='position:relative;left:70px'>of $user[entitlement] remaining</div></td></tr>\n";
		echo "</table><br/>\n";
		echo "Your total annual entitlement is made up from $user[entitlement] days, plus $user[carriedover] carried over from last year.\n";
//		echo "Annual Entitlement: $user[entitlement] days<br/>\n";
//		echo "Days Carried Over: $user[carriedover] days<br/>\n";
//		echo "Total Entitlement: $total days<br/>\n";
//		echo "<br/>Used days this year: $daysUsedThis<br/>\n";
//		echo "Used days next year: $daysUsedNext<br/>\n";
//		echo "<br/>Remaining days this year: $daysRemainThis <input type='hidden' id='thisRemain' value='$daysRemainThis' /><br/>\n";
//		echo "Remaining days next year: $daysRemainNext <input type='hidden' id='nextRemain' value='$daysRemainNext' /><br/>\n";
		echo "<input type='hidden' id='thisRemain' value='$daysRemainThis' /><input type='hidden' id='nextRemain' value='$daysRemainNext' />\n";
		echo "</div></div>\n";
		echo "\n<br/>\n\n";

 // ---------- New Request Section -------------------------------------------------------------------------

		echo "<div style='background:#504E52' class='normalText'>\n";
                echo "<b>New Holiday Request</b><br/><br/>\n";
		echo "<table border='0' cellpadding='10'><tr>\n";
		echo "<td rowspan='2' valign='top'><form action='submitrequest.php' method='post'>Start Date:<div id='startdatepicker' style='font-size:60%;'></div><div align='right'>Start Time: <select id='starttime' name='starttime'><option value='am' selected='selected'>09:00</option><option value='pm'>12:00</option></select></div><input type='hidden' name='startdate' id='startdate' value='". date("Y-m-d"). "' /></td><td rowspan='2' width='25'>\n";
		echo "<td rowspan='2' valign='top'>End Date:<div id='enddatepicker' style='font-size:60%;'></div><div align='right'>End Time: <select id='endtime' name='endtime'><option value='am'>12:00</option><option value='pm' selected='selected'>17:00</option></select></div><input type='hidden' name='enddate' id='enddate' value='". date("Y-m-d"). "'/></td><td rowspan='2' width='25'>\n";
		echo "<td valign='bottom'><br/><br/>Description: <i><small>(Optional)</small></i><br/><input type='text' name='description' size='20' /><br/><br/><br/><a href='calendar.php'><img src='img/calendar.png' height='16' width='16' alt='Holiday Calendar' />Holiday Calendar</a><br/></td>\n";
		echo "</tr><tr><td valign='bottom'><div id='daycount'>Days required for request: 1</div><div id='warningtext'></div><br/><input id='submit' type='submit' value='Submit Request &gt;&gt;' /></form></td></tr></table>\n";
	        echo "</div>\n";
		echo "\n<br/>\n\n";


// ---------- My Holidays Section -------------------------------------------------------------------------

		echo "<div style='background:#504E52' class='normalText'>\n";
		echo "<b>My Holiday Requests</b><br/><br/>\n";

		// TODO limit SQL to only get previous yearsperiods past requests, so we dont end up with loads of lines
		$reqResult = mysqli_query($conn, "SELECT * FROM requests WHERE user='$_SESSION[user]' OR user='' ORDER BY startdate DESC");

		if (mysqli_num_rows($reqResult) > 0)
		{

			echo "<table BORDERCOLORLIGHT='#807E7C' BORDERCOLORDARK='#383634' border='1'><tr><td><b>Description</b></td><td><b>Start Date</b></td><td><b>End Date</b></td><td><b>Days</b></td><td><b>Status</b></td><td><b>Info</b></td><td><b>Cancel</b></td></td>";

			while ($request = mysqli_fetch_array($reqResult))
			{
				// Convert DB am/pm into times to make more logical for humans
				if ($request[starttime] == am)
					$request[starttime] = "09:00";
				if ($request[starttime] == pm)
					$request[starttime] = "12:00";
				if ($request[endtime] == am)
					$request[endtime] = "12:00";
				if ($request[endtime] == pm)
					$request[endtime] = "17:00";
	
				// Set status text colours
				if ($request[status] == "Pending")
					$pColor = "blue";
				if ($request[status] == "Accepted")
					$pColor = "#00FF00";
				if ($request[status] == "Rejected")
					$pColor = "red";
				if ($request[status] == "Cancelled")
					$pColor = "red";
	
				// Highlight newly added request
				if ($_GET[newreq]==$request[requestid] OR $_GET[delreq]==$request[requestid])
					$bg = " bgcolor='#B6010A'";
				else
					$bg = "";
	
				echo "<tr$bg>\n";
				echo "<td>$request[description] &nbsp;</td>\n";
				echo "<td>" . date("d/m/Y", strtotime($request[startdate])) . " $request[starttime]</td>\n";
				echo "<td>" . date("d/m/Y", strtotime($request[enddate])) . " $request[endtime]</td>\n";
				echo "<td>" . str_replace(".0","",$request[days]) . "</td>\n";
				echo "<td style='color:$pColor'><b>$request[status]</b></td>\n";
				echo "<td>$request[statusmessage]</td>\n";
	
				// Disable cancel link for past holidays 
				if (strtotime($request[startdate]) <= strtotime(date("Y-m-d")) OR $request[status] == "Cancelled" OR $request[status] == "Rejected" OR $request[statusmessage] == "Compulsory Holiday")
					echo "<td align='center'><img src='img/nodelete.png' width='16' height='16' /></td></tr>\n";
				else	
					echo "<td align='center'><a href='cancel.php?id=$request[requestid]' onClick='return confirmDelete(\"cancel\");'><img border='0' src='img/delete.png' width='16' height='16' /></a></td></tr>\n";
	
			}
	
			echo "</table>";
		}
		
		else
			echo "<i>You have no holiday requests in the system. You can create one above.</i>";
		
		echo "</div>\n";



	}


	mysqli_free_result($result);

	include("inc/footer.inc.php");

?>
